package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.SchedulingRecordVo;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SchedulingRecord;
import com.ruoyi.system.service.ISchedulingRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 排班记录Controller
 * 
 * @author 华馨悦
 * @date 2025-01-24
 */
@Controller
@RequestMapping("/system/record")
public class SchedulingRecordController extends BaseController
{
    private String prefix = "system/record";

    @Resource
    private ISysUserService userService;

    @Autowired
    private ISchedulingRecordService schedulingRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record(ModelMap mmap)
    {
        mmap.put("users", userService.selectUserList(new SysUser()));
        return prefix + "/record";
    }

    /**
     * 查询排班记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SchedulingRecord schedulingRecord)
    {
        startPage();
        List<SchedulingRecord> list = schedulingRecordService.selectSchedulingRecordList(schedulingRecord);
        List<SchedulingRecordVo> schedulingRecordVos = new ArrayList<>();
        list.forEach(sc -> {
            SysUser sysUser = userService.selectUserById(sc.getEmployeeId());
            SchedulingRecordVo schedulingRecordVo = new SchedulingRecordVo();
            BeanUtils.copyProperties(sc, schedulingRecordVo);
            schedulingRecordVo.setEmployeeName(Optional.ofNullable(sysUser).map(SysUser::getUserName).orElse(""));
            schedulingRecordVos.add(schedulingRecordVo);
        });
        return getDataTable(schedulingRecordVos);
    }

    /**
     * 导出排班记录列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "排班记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SchedulingRecord schedulingRecord)
    {
        List<SchedulingRecord> list = schedulingRecordService.selectSchedulingRecordList(schedulingRecord);
        ExcelUtil<SchedulingRecord> util = new ExcelUtil<SchedulingRecord>(SchedulingRecord.class);
        return util.exportExcel(list, "排班记录数据");
    }

    /**
     * 新增排班记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("users", userService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增保存排班记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "排班记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SchedulingRecord schedulingRecord)
    {
        schedulingRecord.setCreatedTime(DateUtils.getNowDate());
        schedulingRecord.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(schedulingRecordService.insertSchedulingRecord(schedulingRecord));
    }

    /**
     * 修改排班记录
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SchedulingRecord schedulingRecord = schedulingRecordService.selectSchedulingRecordById(id);
        mmap.put("schedulingRecord", schedulingRecord);
        mmap.put("users", userService.selectUserList(new SysUser()));
        return prefix + "/edit";
    }

    /**
     * 修改保存排班记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "排班记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SchedulingRecord schedulingRecord)
    {
        schedulingRecord.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(schedulingRecordService.updateSchedulingRecord(schedulingRecord));
    }

    /**
     * 删除排班记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "排班记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(schedulingRecordService.softDeleteSchedulingRecordByIds(ids));
    }
}
